package com.xc.erp.modules.erp.controller.tenant;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.modules.erp.domain.AjaxResult;
import com.xc.erp.modules.erp.domain.pojo.JshFunction;
import com.xc.erp.modules.erp.domain.pojo.JshTenant;
import com.xc.erp.modules.erp.service.JshTenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = {"租户管理"})
@RestController
@RequestMapping("/tenant")
public class TenantController {

    @Autowired
    private JshTenantService jshTenantService;

    @ApiOperation("查询租户列表")
    @GetMapping("list/{pageSize}/{pageNum}")
    public AjaxResult list(@PathVariable Integer pageSize,
                           @PathVariable Integer pageNum, String loginName){
        Page<JshTenant> list = jshTenantService.list(pageSize, pageNum, loginName);
        return AjaxResult.success(list);
    }
    @ApiOperation("查询租户列表")
    @GetMapping("list")
    public AjaxResult list(){
        List<JshTenant> list = jshTenantService.list();
        return AjaxResult.success().put("tenantList",list);
    }
    @Log(modul = "租户管理",desc = "新增")
    @ApiOperation("新增功能")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody JshTenant jshTenant){
        jshTenant.setEnabled(true);
        jshTenant.setCreateTime(new Date());
        jshTenantService.save(jshTenant);
        return AjaxResult.success();
    }

    @ApiOperation("检查名称是否存在")
    @GetMapping("/checkIsNameExist")
    public AjaxResult checkIsNameExist(Long id,String loginName){
        boolean status = jshTenantService.checkIsNameExist(id,loginName);
        return AjaxResult.success().put("status",status);
    }

    @Log(modul = "租户管理",desc = "编辑")
    @ApiOperation("编辑功能")
    @PutMapping("update")
    public AjaxResult update(@RequestBody JshTenant jshTenant){
        jshTenantService.updateById(jshTenant);
        return AjaxResult.success();
    }

    @Log(modul = "租户管理",desc = "删除")
    @ApiOperation("删除功能")
    @DeleteMapping("delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        jshTenantService.removeById(id);
        return AjaxResult.success();
    }

    @Log(modul = "租户管理",desc = "批量删除")
    @ApiOperation("批量删除")
    @DeleteMapping("deleteBatch")
    public AjaxResult deleteBatch(@RequestBody Long[] ids){
        jshTenantService.removeBatchByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @Log(modul = "租户管理",desc = "批量修改状态")
    @ApiOperation("批量修改状态")
    @PutMapping("batchSetStatus/{status}")
    public AjaxResult batchSetStatus(@PathVariable boolean status,@RequestBody Long[] ids){
        jshTenantService.batchSetStatus(status,ids);
        return AjaxResult.success();
    }
}
